import 'package:flutter/material.dart';


class FormOne extends StatefulWidget {
  const FormOne({Key? key}) : super(key: key);

  @override
  State<FormOne> createState() => _FormOneState();
}

class _FormOneState extends State<FormOne> {
  TextEditingController _unameController = new TextEditingController();
  TextEditingController _pwdController = new TextEditingController();
  //FormState为Form的State类，可以通过 Form.of() 或GlobalKey获得。我们可以通过它来对
   //Form的子孙FormField进行统一操作。
  final _formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:Text("用户登录表单")
      ),
      /**
       * 满足以下条件
       * 用户名不为空
       * 密码长度大于6位
       * 可清空
       */
      body: Form(
        key: _formKey, //设置GlobalKey，用于后面获取FormState
        onChanged: (){
          print('change');
        },
        child: Padding(
          padding: EdgeInsets.all(20),
          child:Column(
            crossAxisAlignment:CrossAxisAlignment.center,
            children: [
              TextFormField(
                controller: _unameController,
                decoration: InputDecoration(
                  icon: Icon(Icons.person_outline),
                  labelText: "用户名"
                ),
                validator: (value){
                  return value!.isEmpty ? '用户名不能为空' : null;
                },
                onSaved: (data){
                  print('save:$data');
                },
               // initialValue: 'user',// 默认值
                enabled: true,
              ),
              TextFormField(
              controller: _pwdController,
              decoration: InputDecoration(
                icon:Icon(Icons.lock_outline),
                labelText: '密码',
              ),
              obscureText: true,
              validator: (value){
                var isNum = isNumber(value!);
                if(!isNum){
                  return "密码必须是数字";
                }
                if (value!.isEmpty) {
                  return '密码不能为空';
                }
                if (value.length <= 6) {
                  return '密码不能低于6位';
                }
                return null;
              },
            ),
            Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Row(
                  children: <Widget>[
                    Expanded(
                      child: RaisedButton(
                        onPressed: () {
                          _formKey.currentState?.reset();
                        },
                        child: Text('清空'),
                      ),
                    )
                  ],
                )
            ),
            Padding(
              //symmetric代表着对称，其vertical代表上下对称，horizontal代表左右对称
              padding: EdgeInsets.symmetric(vertical: 8.0),
              child: Row(
                children: [
                  Expanded(
                    child: RaisedButton(
                      color: Theme.of(context).primaryColor,
                      textColor: Colors.white,
                      onPressed: (){
                        if(_formKey.currentState!.validate()){
                           //验证通过提交数据
                           String user = _unameController.text;
                           String pwd = _pwdController.text;
                           print("用户名是$user,密码是$pwd");
                           ScaffoldMessenger.of(context).showSnackBar(
                            SnackBar(content: Text("验证通过！"))
                           );
                        }
                      },
                      child: Text("登录"),
                    )
                  )
                ],
              ),
              )
            ],
          )
        )
      ),
    );
    
  }
}

bool isNumber(String str){
   final reg = RegExp(r'^-?[0-9]+');
    print('$str 是否数字:${reg.hasMatch(str)}');
    return reg.hasMatch(str);
}
